//
//  TSMainSectionHeaderView.swift
//  TSProfitProject
//
//  Created by TmmmS on 2019/11/1.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit
import HandyJSON

protocol TSMainSectionHeaderViewDelegate : NSObjectProtocol {
    func didClickHeaderEntry(title:String)
}

struct TSAnnouncementModel : HandyJSON {
    var notice : String?
}

class TSMainHeaderEntry: UIView {
    
    var title : String!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    convenience init(title: String) {
        self.init()
        self.title = title
        configView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    func configView() {
        
        let icon = UIImageView(image: UIImage(named: "main_entry_bg"))
        addSubview(icon)
        icon.snp.makeConstraints { (make) in
            make.centerX.top.equalToSuperview()
            make.size.equalTo(CGSize(width: 100, height: 32))
        }
        
        let titleLabel = UILabel()
        titleLabel.text = title
        titleLabel.textColor = WHITE_COLOR
        titleLabel.font = UIFont.systemFont(ofSize: 13)
        titleLabel.textAlignment = .center
        icon.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
    }
}

class TSMainSectionHeaderView: UIView {
    
    weak var delegate : TSMainSectionHeaderViewDelegate?
    var paomaView : TTPaomaView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        configView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func tapGesture(gesture:UITapGestureRecognizer) {
        
        let entryItem = gesture.view as? TSMainHeaderEntry
        if self.delegate != nil {
            self.delegate?.didClickHeaderEntry(title: entryItem!.title)
        }
        
    }
    
    func setupNoticeView(array:Array<TSAnnouncementModel>) {
        
        var tempArray : Array<String> = []
        for item in array {
            tempArray.append(item.notice ?? "")
        }
        let titleArray = tempArray as NSArray
        paomaView.setText(text: titleArray.componentsJoined(by: " "))
    }
    
    func configView() {
        
        self.backgroundColor = WHITE_COLOR
        
        let icon = UIImageView.init(image: UIImage(named: "main_notice"))
        addSubview(icon)
        icon.snp.makeConstraints { (make) in
            make.left.equalTo(12)
            make.top.equalToSuperview()
            make.size.equalTo(CGSize(width: 20, height: 20))
        }
        
        paomaView = TTPaomaView.init(frame: CGRect(x: 37, y: 3, width: SCREEN_WIDTH - 2*12 - 25, height: 15))
        paomaView.setText(text: "暂无")
        paomaView.backgroundColor = WHITE_COLOR
        paomaView.textColor = BASE_GRAY_COLOR
        addSubview(paomaView)
        
        let container = UIView.init()
        container.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#F7F7F7")
        addSubview(container)
        container.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(icon.snp.bottom).offset(10)
        }
        
        let entryContainer = UIView.init()
        entryContainer.backgroundColor = WHITE_COLOR
        container.addSubview(entryContainer)
        entryContainer.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalTo(-10)
            make.height.equalTo(55)
        }
        
        var entryArray = [TSMainHeaderEntry]()
        let titleArray = ["充值", "提现", "额度转换"]
        for index in 0..<titleArray.count {
            let headerEntry = TSMainHeaderEntry.init(title: titleArray[index])
            headerEntry.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(tapGesture(gesture:))))
            entryContainer.addSubview(headerEntry)
            entryArray.append(headerEntry)
        }
        
        entryArray.snp.distributeViewsAlong(axisType: .horizontal, fixedItemLength: 100*T_ScreenScale(), leadSpacing: 15, tailSpacing: 15)
        entryArray.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
//            make.top.equalTo(paomaView.snp.bottom).offset(10)
            make.height.equalTo(32)
        }
    }

}
